package org.example.org.service.impl;


import org.example.base.service.impl.BaseServiceImpl;
import org.example.org.domain.Department;
import org.example.org.mapper.DepartmentMapper;
import org.example.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Objects;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;

    @Transactional
    @Override
    public void insert(Department department) {

        mapper.insert(department);
        if(Objects.nonNull(department.getParent().getId())){
            Department parent = selectById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());

        }else{
            department.setPath("/"+department.getId());
        }
        mapper.update(department);
    }
    @Transactional
    @Override
    public void update(Department department) {
        if(Objects.nonNull(department.getParent().getId())){
            Department parent = selectById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());

        }else{
            department.setPath("/"+department.getId());
        }
        mapper.update(department);
    }



    @Override
    public List<Department> getDeptTree() {
        return mapper.getDeptTree();
    }
}
